#include "stdafx.h"	
#include "Client.h"

int _tmain()
{
	fprintf(stdout, "Client has started\n");
	
	WSADATA wsaData;
	int wsResult = WSAStartup(MAKEWORD(2,2), &wsaData);
	if (wsResult != 0) {
		fprintf(stdout, "WSAStartup failed with error: %d\n", wsResult);		
		return -1;
	}

	// send alarms
	for(int i = 0; i <= 10; i++) {
		unsigned int port =  DEFAULT_PORT; //alarm port
		Network::INET_Addr *address = new Network::INET_Addr(DEFAULT_SERVER, port);
		Network::SOCK_Stream *serverStream = new Network::SOCK_Stream();
		Network::SOCK_Connector *connector = new Network::SOCK_Connector();

		AlarmEvent event;
		event.priority = rand() % 10 + 1; // random number between 1 and 10
		event.text = "AlarmEvent. It works!";

		u_int buffer_size = getAlarmBufferSize(event);
		char* buffer = (char*) malloc(buffer_size);
		
		if (serializeAlarmEvent(event, buffer) != 0) {
			fprintf(stdout, "Error while serializing !\n");		
		}

		sendDataToServer(address, serverStream, connector, buffer, buffer_size);

		free(buffer);
	}
	
	// send log events
	for(int i = 0; i <= 10; i++) {
		unsigned int port =  DEFAULT_PORT; //log port
		Network::INET_Addr *address = new Network::INET_Addr(DEFAULT_SERVER, port);
		Network::SOCK_Stream *serverStream = new Network::SOCK_Stream();
		Network::SOCK_Connector *connector = new Network::SOCK_Connector();

		LogEvent event;
		
		event.text = "LogEvent. It works!";
		u_int buffer_size = getLogBufferSize(event);
		char* buffer = (char*) malloc(buffer_size);
		//ZeroMemory(buffer, buffer_size);

		if (serializeLogEvent(event, buffer) != 0) {
			fprintf(stdout, "Error while serializing !\n");		
		}

		sendDataToServer(address, serverStream, connector, buffer, buffer_size);

		free(buffer);
	}
	
	// send patient events
	for(int i = 0; i <= 10; i++) {
		unsigned int port =  DEFAULT_PORT; //log port
		Network::INET_Addr *address = new Network::INET_Addr(DEFAULT_SERVER, port);
		Network::SOCK_Stream *serverStream = new Network::SOCK_Stream();
		Network::SOCK_Connector *connector = new Network::SOCK_Connector();

		PatientEvent event;
		event.type = rand() % 10 + 1; // random number between 1 and 10
		event.value = "Patient OK!";
		u_int buffer_size = getPatientBufferSize(event);
		char* buffer = (char*) malloc(buffer_size);
		//ZeroMemory(buffer, buffer_size);

		if (serializePatientEvent(event, buffer) != 0) {
			cout << "Error while serializing !\n";		
		}

		sendDataToServer(address, serverStream, connector, buffer, buffer_size);

		free(buffer);
	}
	WSACleanup();
	system("pause");
}

void sendDataToServer(Network::INET_Addr *address, 
		Network::SOCK_Stream *serverStream, 
		Network::SOCK_Connector *connector, char* buffer, u_int buffer_size) 
{
		bool connected = connector->connect_to_server(*serverStream, *address);
		if (connected) {
			int bytesSent = serverStream->send(buffer, buffer_size, 0);

			fprintf(stdout, "Data sent to the server. Size = %d; content =\n", bytesSent);
			for(u_int i = 0; i < buffer_size; ++i){
				char term = i < buffer_size-1 ? ',' : '\n'; //add commas between entries, add new line at the end
				fprintf(stdout, "0x%02x%c ", buffer[i], term);
			}

			serverStream->close();
		} else {
			fprintf(stdout, "Failed connecting. \n");
			int error = WSAGetLastError();
			fprintf(stdout, "WSA error: %d\n", error);
		}
}